﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YY.Common;
using System.Collections;
using YY.UI.BaseDataSvc;
using System.Reflection;
using System.Diagnostics;
using System.Configuration;

namespace YY.UI
{
    public partial class MainFrm : BaseFrm
    {
        public MainFrm()
        {
            InitializeComponent();
            this.Text = ConfigurationManager.AppSettings["SysTitle"] + "业务管理系统";
            this.BaseDataProxy = new YY.UI.BaseDataSvc.BaseDataClient();
        }

        private void MainFrm_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            this.BaseDataProxy.Open();

            this.toolStripStatusLabel1.Text = "用户：" + SysLoginUserInfo.UserName;
            this.toolStripStatusLabel2.Text = "角色：" + Enum.GetName(typeof(EnumCommon.SysRoleType), SysLoginUserInfo.Role);
            this.toolStripStatusLabel3.Text = "登录时间：" + SysLoginUserInfo.LoginTime.ToString("yyyy-MM-dd hh:mm:ss");

            ArrayList NavItems = new ArrayList();


            IEnumerable<TB_SysModule> datas = this.BaseDataProxy.GetUserMenus();
            if (datas != null)
            {
                datas.OrderBy(p => p.menuOrder);
                var menuBars = from p in datas group p by p.pMenuName into g select g;
                int count = 0;
                foreach (var s in menuBars)
                {
                    var menus = datas.Where(p => p.pMenuName == s.Key);
                    if (menus.Any())
                    {
                        TreeView tv = new TreeView();
                        foreach (TB_SysModule menu in menus)
                        {
                            TreeNode node = new TreeNode(menu.moduleName);
                            node.Name = menu.frmId;
                            tv.Nodes.Add(node);
                        }
                        NavMenuBar.NavItem nv = new NavMenuBar.NavItem(s.Key, s.Key, tv, count == 0);
                        NavItems.Add(nv);
                        count++;
                    }
                }

            }
            this.navMenuBar1.MenuItems = NavItems;
            this.navMenuBar1.RenderMenu();
        }

        private void 密码修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {            
            Common.ShowMDIChildForm(typeof(ResetPwdFrm), this);           
        }


        private void 工作单查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Common.ShowMDIChildForm(typeof(OrderFrm), this);           
        }

        private void navMenuBar1_OnTreeNodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            StackTrace ss = new StackTrace(true);
            MethodBase mb = ss.GetFrame(1).GetMethod();
            string ns = mb.DeclaringType.Namespace;
            Assembly assem = Assembly.Load(ns);
            Type t = assem.GetType(ns + "." + e.Node.Name);
            //Form frm = (Form)assem.CreateInstance(ns + "." + e.Node.Name);
            // .IsPermitFrm = true;
            //if (!Common.MdiFrmIsOpened(this, frm.Name))
            //{
            //    frm.MdiParent = this;
            //    frm.Parent = this.splitContainerEx1.Panel2;
            //    frm.Show();
            //    frm.Focus();
            //}
            Common.ShowMDIChildForm(t, this);
        }

        private void cascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void tileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void tileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void closeAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void arrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

    }
}
